/*******************************************************************************
 * Copyright (c) 2008 Sylvain Sicard.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Contributors:
 *     Sylvain Sicard - initial API and implementation
 ******************************************************************************/
package fr.jade.resources.nfs;

import java.net.InetAddress;
import java.net.UnknownHostException;

import org.objectweb.fractal.api.control.IllegalLifeCycleException;
import org.objectweb.fractal.api.control.LifeCycleController;

import fr.jade.base.JadeBaseComponent;
import fr.jade.fraclite.api.control.NoSuchAttributeException;
import fr.jade.resources.api.NfsConnector;
import fr.jade.util.JadeException;
import fr.jade.util.ShellCommand;

public class ServerWrapper extends JadeBaseComponent implements NfsConnector, LifeCycleController {

  // Attributes
  protected static final String exportAttName = "export";
  protected String export;
  
  protected static final String portAttName = "port";
  protected int port;
  
  protected static final String[] listAtt = {exportAttName, portAttName};
  
  // local attributes
  boolean started = false;
  
  LifeCycleController client = null;
  
  public ServerWrapper(){
    
  }
  
  @Override
  public String getFcState() {
    return started?STARTED:STOPPED;
  }

  @Override
  public void startFc() throws IllegalLifeCycleException {
    if(!started){
      started = true;
      String srvCmd = "/sbin/service nfs start";
      String exportCmd = "/usr/sbin/exportfs -i *:"+export;
      log(srvCmd);
      log(exportCmd);
      try {
        ShellCommand.syncExec(srvCmd, new String[0]);
        ShellCommand.syncExec(exportCmd, new String[0]);
        
        if(client != null){
          client.startFc();
          client = null;
        }
      } catch (JadeException e) {
        e.printStackTrace();
      }
    }
  }

  @Override
  public void stopFc() throws IllegalLifeCycleException {
    if(started){
      started = false;
      log("Server stop");
      String exportCmd = "/usr/sbin/exportfs -u *:"+export;
      String srvCmd = "/sbin/service nfs stop";
      log(exportCmd);
      log(srvCmd);
      try {
        ShellCommand.syncExec(exportCmd, new String[0]);
        ShellCommand.syncExec(srvCmd, new String[0]);
      } catch (JadeException e) {
        e.printStackTrace();
      }
    }
  }

  @Override
  public String getAttribute(String name) throws NoSuchAttributeException {
    if(name.equals(exportAttName)){
      return export;
    }else if(name.equals(portAttName)){
      return Integer.toString(port);
    }else{
      return super.getAttribute(name);
    }
  }
  
  @Override
  public String[] listFcAtt() {
    return super.concat(listAtt, super.listFcAtt());
  }

  @Override
  public void setAttribute(String name, String value) throws NoSuchAttributeException {
    if(name.equals(exportAttName)){
      export = value;
    }else if(name.equals(portAttName)){
      port = Integer.parseInt(value);
    }else{
      super.setAttribute(name, value);
    }
  }

  @Override
  public String getExport() {
    return export;
  }

  @Override
  public String getHost() {
    String host=null;
    try {
      host = InetAddress.getLocalHost().getHostName();
    } catch (UnknownHostException e) {
      e.printStackTrace();
    }
    return host;
  }

  @Override
  public int getPort() {
    return port;
  }

  @Override
  public boolean syncStart(LifeCycleController lcc) {
    if(!started){
      client = lcc;
    }
    return started;
  }

}
